package org.dalmatian.common.redis.interceptor;

import org.dalmatian.common.redis.annotation.*;
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;



public class CacheInterceptor implements Interceptor {


    @Override
    public Object doIntercept(Invocation inv) throws Throwable {
        Cacheable cacheable = inv.getMethodAnnotation(Cacheable.class);
        CacheEvict cacheEvict = inv.getMethodAnnotation(CacheEvict.class);
        CachePut cachePut = inv.getMethodAnnotation(CachePut.class);
        Object result = null;
        if (cacheable != null) {
           result =  CacheExecutor.global.cacheAble(cacheable,inv, inv::invoke);
        }
        if (cachePut != null) {
            result =  inv.invoke();
            CacheExecutor.global.cachePut(cachePut,inv,result);
        }
        if(cacheEvict != null){
            result =  inv.invoke();
            CacheExecutor.global.cacheEvict(cacheEvict,inv,result);
        }
        return result;
    }

}
